
package com.sbtr.business.calibration.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.business.calibration.entity.CalibrationTxtEntity;
import com.sbtr.business.calibration.mapper.CalibrationTxtEntityMapper;
import com.sbtr.business.calibration.service.ICalibrationTxtEntityService;
import com.sbtr.common.PageDto;
import com.sbtr.open.calibration.dto.CalibrationCountByFinisherDto;
import com.sbtr.open.calibration.dto.CalibrationDetailsDto;
import com.sbtr.open.calibration.dto.CalibrationSearchByDetailsDto;
import com.sbtr.open.calibration.dto.CalibrationTxtEntityDto;
import com.sbtr.util.BeanCopyUtils;
import com.sbtr.util.PageDtoUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;


/**
 * <p>
 * 标定集 服务实现类
 * </p>
 */
@Transactional
@Service
public class CalibrationTxtEntityServiceImpl extends ServiceImpl<CalibrationTxtEntityMapper, CalibrationTxtEntity>
        implements ICalibrationTxtEntityService {

    private static final Logger logger = LoggerFactory.getLogger(CalibrationTxtEntityServiceImpl.class);

    @Autowired
    private CalibrationDetailsServiceImpl calibrationDetailsService;

    @Resource(name = "asyncTxtExecutor")
    Executor asyncExecutor;

    @Override
    public boolean batchSave(List<CalibrationTxtEntityDto> dtos) {
        List<CalibrationTxtEntity> materialtxt = BeanCopyUtils.copyProperties(dtos, CalibrationTxtEntity.class);
        if (materialtxt.size() < 10000) {
            saveBatch(materialtxt);
        } else {
            int num = 2000;
            int k = 0;
            List<CalibrationTxtEntity> saveList = new ArrayList<>();
            ;
            for (int i = 0; i < materialtxt.size(); i++) {
                k++;
                saveList.add(materialtxt.get(i));
                if (k == num || i - 1 == materialtxt.size()) {
                    k = 0;
                    batchSaveAsync(saveList);
                    saveList = new ArrayList<>();
                }
            }
        }
        return true;
    }

    @Override
    public List<CalibrationTxtEntityDto> searchHasCalibratedByIds(Long id) {
        QueryWrapper<CalibrationTxtEntity> wrapper = new QueryWrapper<>();
        wrapper.in("calibration_id", id);
        wrapper.eq("completed", "1");
        List<CalibrationTxtEntity> txt = list(wrapper);
        return BeanCopyUtils.copyProperties(txt, CalibrationTxtEntityDto.class);
    }

    @Override
    public List<CalibrationTxtEntityDto> searchHasCalibratedByIdsAndFinisher(Long id,
                                                                             String finisher) {
        QueryWrapper<CalibrationTxtEntity> wrapper = new QueryWrapper<>();
        wrapper.in("calibration_id", id);
        wrapper.eq("finisher", finisher);
        wrapper.eq("completed", "1");
        List<CalibrationTxtEntity> txt = list(wrapper);
        return BeanCopyUtils.copyProperties(txt, CalibrationTxtEntityDto.class);
    }

    @Override
    public PageDto<CalibrationTxtEntityDto> getTxtEntityCalibrate(CalibrationSearchByDetailsDto dto) {
        IPage<CalibrationTxtEntity> page = new Page<>();
        page.setSize(dto.getPageSize());
        page.setCurrent(dto.getPage());
        QueryWrapper<CalibrationTxtEntity> wrapper = new QueryWrapper<>();

        CalibrationDetailsDto calibrationDetailsDto = calibrationDetailsService.findById(dto.getCalibrationDetailId());
        if (null != dto.getId()) {
            wrapper.eq("calibration_id", calibrationDetailsDto.getCalibrationId());
        }
        if (StringUtils.isNotEmpty(dto.getFinisher())) {
            wrapper.eq("finisher", calibrationDetailsDto.getFinisher());
        }
//		if(null !=dto.getCalibrationDetailId()){
//			wrapper.eq("calibration_detail_id",dto.getCalibrationDetailId());
//		}
        if (StringUtils.isNotEmpty(dto.getCompleted())) {
            wrapper.eq("completed", dto.getCompleted());
        }

//		wrapper.orderByDesc("id");
        page = page(page, wrapper);
        PageDto<CalibrationTxtEntityDto> pageDto = PageDtoUtils.getPageDtoByIPage(page, CalibrationTxtEntityDto.class);
        return pageDto;
    }

    @Override
    public List<CalibrationCountByFinisherDto> countTaskByCalibrationIds(Long id) {
        return baseMapper.countTaskByCalibrationIds(id);
    }

    @Async("asyncTxtExecutor")
    public void batchSaveAsync(List<CalibrationTxtEntity> materialTxtList) {
        asyncExecutor.execute(new batchSaveThread(materialTxtList));
    }

    @Async
    class batchSaveThread implements Runnable {

        private List<CalibrationTxtEntity> materialTxtList;

        public batchSaveThread(List<CalibrationTxtEntity> list) {
            this.materialTxtList = list;
        }

        @Override
        public void run() {
            saveBatch(materialTxtList);
        }
    }
}